//
//  MainCell.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/7.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "MainCell.h"
#import "ImageCell.h"
#import <AVOSCloud/AVOSCloud.h>
#import  <UIImageView+AFNetworking.h>
@interface MainCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *headerView;

@property (weak, nonatomic) IBOutlet UILabel *nameL;
@property (weak, nonatomic) IBOutlet UILabel *titleL;

@property (weak, nonatomic) IBOutlet UICollectionView *imageColldctionView;

@property (nonatomic, strong) NSArray *imageArr;
@property (weak, nonatomic) IBOutlet UILabel *padent;
@property (weak, nonatomic) IBOutlet UILabel *timeL;
@property (weak, nonatomic) IBOutlet UILabel *state;


@end


@implementation MainCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _imageColldctionView.scrollEnabled = NO;
    _imageColldctionView.delegate = self;
    _imageColldctionView.dataSource = self;
    _headerView.layer.cornerRadius = 12.5;
    _headerView.layer.masksToBounds = YES;
    
}

-(void)setNews:(HomeModel *)news{
    _news = news;

    _padent.text = _news.grade;
//
    _nameL.text = _news.name;
    [_headerView setImageWithURL:[NSURL URLWithString:_news.image]];
    _state.text = _news.state;
    
//    AVQuery *query = [AVQuery queryWithClassName:@"_User"];
//  //  [query includeKey:@"owner"];
//
//    [query whereKey:@"username" equalTo:_news.owner];
//    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
//        if (!error) {
//                        for (AVObject *obj in objects) {
//                            NSString *str = [obj objectForKey:@"image"];
//                            if (str != nil){
//                                [_headerView setImageWithURL:[NSURL URLWithString:str]];
//                            }else{
//                                _headerView.backgroundColor = [UIColor blueColor];
//                            }
//                            
//                            _nameL.text = [obj objectForKey:@"name"];
//            
//                            
//                        }
//                    }
//                }];
//    
//
//    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
//        if (!error) {
//            for (AVObject *obj in objects) {
//                AVUser *user = [obj objectForKey:@"owner"];
//                 
//               
//                [_headerView setImageWithURL:[NSURL URLWithString:[obj objectForKey:@"imageT"]]];
//                _nameL.text = [obj objectForKey:@"name"];
//                
//                
//            }
//        }
//    }];

    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    _timeL.text = [dateFormatter stringFromDate:_news.createdAt];
    _titleL.text = [NSString stringWithFormat:@"%@\n %@",_news.location,_news.particulars];
    if (_isOk) {
        _imageArr = _news.imageArr;
        [self.imageColldctionView reloadData];
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.imageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    NSString *image = self.imageArr[indexPath.row];
    cell.image = image;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.scrollerBlock();
}


@end
